﻿import com.bourre.commands.Delegate;
import com.bourre.events.BasicEvent;
import com.bourre.events.EventBroadcaster;
import com.bourre.events.EventType;
/**
 * @author Chris McKenzie 
 * @desc A static Style Class for Loading and applying StyleSheets to TextFields
 * @modfiedby Chris Edwards
 * converted to Singleton
 */


class com.mrmny.global.TextStyle extends EventBroadcaster {
	
	
	// EventList
	public static var onLoadedEVENT		:EventType = new EventType("onLoaded");
	public static var onLoadErrorEVENT	:EventType = new EventType("onLoadError");
	//
	
	// Singleton implementation

	private static var _instance:TextStyle;
	
	public static function getInstance():TextStyle{
		if(!TextStyle._instance) TextStyle._instance = new TextStyle();
		return TextStyle._instance;	
	}
	public static function get instance():TextStyle{return TextStyle.getInstance();};
	
	
	//Static util functions

	static function getStyledString(text:String, style:String):String {
		if(style){
			return '<span class="'+style+'">'+text+'</span>';
		}else {
			return text;	
		}
	}
	
	public static function createHeadline(parent:MovieClip,name:String,depth:Number):TextField{
	
		if(depth==undefined) depth = parent.getNextHighestDepth();
		
		var tf:TextField = parent.createTextField(name,depth,0,0,0,0);

		tf.multiline=false;
		tf.autoSize="left";
		tf.selectable=false;
		tf.antiAliasType="advanced";
		
		return tf;	
	}
	
	public static function createParagraph(parent:MovieClip,name:String,depth:Number,width:Number):TextField{
		
		if(!width) width = 300;
		if(depth==undefined) depth = parent.getNextHighestDepth();
		
		var tf:TextField = parent.createTextField(name,depth,0,0,width,0);

		tf.multiline=true;
		tf.autoSize="left";
		tf.wordWrap = true;
		tf.selectable=false;
		tf.antiAliasType="advanced";

		return tf;	
	}
	
	
	// Private instance properties
	
	private var _css:TextField.StyleSheet;
	private var _isLoaded:Boolean;
	
	
	private function TextStyle(){
		super();	
	}
	
	// Private functions
	
	private function _onLoad(bSuccess:Boolean):Void{
		
		var event:BasicEvent = new BasicEvent(TextStyle.onLoadErrorEVENT,this);
		
		this._isLoaded= bSuccess;
		
		if(bSuccess){
			event.setType(TextStyle.onLoadedEVENT);		
		}
		
		this.broadcastEvent(event);	
	}
	
	// Public functions
	
	public function loadCSS(url:String):Void {
		if(!this._css) this._css = new TextField.StyleSheet();
		this._css.onLoad = Delegate.create(this,this._onLoad); 
		this._css.load(url);
	}
	
	public function setStyle(styleName:String, styleObj:Object):Void {
		if(!this._css) this._css = new TextField.StyleSheet();
		this._css.setStyle(styleName, styleObj);
	}
		
	public function apply(tf:TextField, text:String, style:String):Void {
		this.setupTextField(tf);
		tf.htmlText=TextStyle.getStyledString(text,style);
	}
	
	public function append(tf:TextField, text:String, style:String):Void {
		this.setupTextField(tf);
		tf.htmlText+=TextStyle.getStyledString(text,style);
	}
	
	public function setupTextField(tf:TextField):Void {
		tf.html = true;
		tf.embedFonts = true;
		tf.styleSheet = this._css;
	}
	
	
	//read only
	public function get isLoaded():Boolean{return this._isLoaded;};
	
	
	
	
	
}
